package zym.diandian.tools;


import android.app.ActivityManager;
import android.content.Context;
import android.hardware.Camera;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HardwareHelper {

    public static boolean externalMemoryAvailable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    //获取外置存储可用空间
    public static long getAvailableExternalMemorySize() {
        if (HardwareHelper.externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return availableBlocks * blockSize;
        } else {
            return -1;
        }
    }

    //获取内置存储可用空间
    public static long getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }

    //获取可用内存大小
    private static String getAvailMemory(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);
        return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
    }

    private static List<Camera.Size> getCamera(int cameraid) {
        List<Camera.Size> li2 = new ArrayList<Camera.Size>();
        Camera camera = null;
        if (Build.VERSION.SDK_INT < 9) {
            camera = Camera.open();
        } else {
            camera = Camera.open(cameraid);
        }
        li2 = camera.getParameters().getSupportedPictureSizes();
        camera.release();
        return li2;
    }

    public static String getCameraSize(int cameraid) {

        int cameras = Camera.getNumberOfCameras();
        if ((Build.VERSION.SDK_INT < 9) && (cameraid == Camera.CameraInfo.CAMERA_FACING_FRONT)) {
            return "未知";
        }
        List<Camera.Size> list = getCamera(cameraid);
        return list.get(0).width + "x" + list.get(0).height;

    }

    public static String[] getJBXX(Context context) throws Exception {
        String[] result = new String[9];
        Process process = Runtime.getRuntime().exec("getprop ro.product.brand");
        result[0] = NetUtil.inputStream2String(process.getInputStream()).trim();
        process = Runtime.getRuntime().exec("getprop ro.product.model");
        result[1] = NetUtil.inputStream2String(process.getInputStream()).trim();
        process = Runtime.getRuntime().exec("getprop ro.build.version.release");
        result[2] = NetUtil.inputStream2String(process.getInputStream()).trim();
        result[3] = HardwareHelper.getCameraSize(0);
        result[4] = HardwareHelper.getCameraSize(1);
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = telephonyManager.getDeviceId();
        if (TextUtils.isEmpty(imei)) {
            process = Runtime.getRuntime().exec("getprop persist.radio.imei");
            result[5] = NetUtil.inputStream2String(process.getInputStream());
        } else {
            result[5] = imei;
        }
        result[6] = "" + HardwareHelper.getAvailMemory(context) + "/" + HardwareHelper.getTotalMemory(context);

        long totalInternalMemorySize = HardwareHelper.getTotalInternalMemorySize();
        long availableInternalMemorySize = HardwareHelper.getAvailableInternalMemorySize();

        long totalExternalMemorySize = HardwareHelper.getTotalExternalMemorySize();
        long availableExternalMemorySize = HardwareHelper.getAvailableExternalMemorySize();
        result[7] = "" + Formatter.formatFileSize(context, totalInternalMemorySize) + "\r\r(可用\r：" + Formatter.formatFileSize(context, availableInternalMemorySize) + ")";
        result[8] = "" + Formatter.formatFileSize(context, totalExternalMemorySize) + "\r\r(可用\r：" + Formatter.formatFileSize(context, availableExternalMemorySize) + ")";

        process.destroy();

        return result;
    }

    //获取外置存储总空间
    public static long getTotalExternalMemorySize() {
        if (HardwareHelper.externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize;
            long totalBlocks;
            if (Build.VERSION.SDK_INT >= 18) {
                blockSize = stat.getBlockSizeLong();
                totalBlocks = stat.getBlockCountLong();
            } else {
                blockSize = stat.getBlockSize();
                totalBlocks = stat.getBlockCount();
            }
            return totalBlocks * blockSize;
        } else {
            return -1;
        }
    }

    //获取内置存储总大小
    public static long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize;
        long totalBlocks;
        if (Build.VERSION.SDK_INT >= 18) {
            blockSize = stat.getBlockSizeLong();
            totalBlocks = stat.getBlockCountLong();
        } else {
            blockSize = stat.getBlockSize();
            totalBlocks = stat.getBlockCount();
        }
        return totalBlocks * blockSize;
    }

    private static String getTotalMemory(Context context) {
        String str1 = "/proc/meminfo";// 系统内存信息文件
        String str2;
        String[] arrayOfString;
        long initial_memory = 0;

        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
            str2 = localBufferedReader.readLine();// 读取meminfo第一行，系统总内存大小

            arrayOfString = str2.split("\\s+");
            for (String num : arrayOfString) {
                Log.i(str2, num + "\t");
            }

            initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存，单位是KB，乘以1024转换为Byte
            localBufferedReader.close();

        } catch (IOException e) {
        }
        return Formatter.formatFileSize(context, initial_memory);// Byte转换为KB或者MB，内存大小规格化
    }
}
